 var saveFlag ;
 var serverpath;
 var quotationId;
	$(document).ready(function(){
		 saveFlag = true;
		 serverpath = $("#server").val();
		 quotationId = $("#quotationId").val();
			 
	});
	function reviewMerchandiselist(){
		window.location.href=serverpath+"/merchandise/list/"+quotationId+"/-1";
	}
	function orderlist(){
		window.location.href=serverpath+"/quotationorder/list/"+quotationId;
	}
	function getList(skuStatu){
		window.location.href=serverpath+"/quotationmerchandise/list/"+quotationId+"/"+skuStatu;
	}
	function  catchFromInt(obj){
		var flag = true;
		 if(flag){
			if(confirm('未抓取到数据，请手动填写')){
				var bbb = obj.closest('.merch');
				//bbb.contents().find('input').setAttribute("style","disabled:disabled");
				//var id = bbb.setAttribute("style","display:none");
			}
		} 
	}
	function pushMerchandise(row){
		setMessage("alert-success","提交请求中......");
		
		var url = serverpath+"/quotationmerchandise/upMerchandise/"+quotationId+"/"+row;
		$.ajax({
			type: "POST",
			url:url,
	        dataType:'text',
	        data: {'status':'3'},
			success: function(data){
				if("success"==data){
					setMessage("alert-success","提交成功，等待审核！2秒后将刷新页面");
					window.setTimeout(function (){
		       			window.location.href=serverpath+"/quotationmerchandise/list/"+quotationId+"/"+$('#skuStatu').val();
		       		},2000);
				}else{
					setMessage("alert-danger","操作失败！");
				}
	       		
	      	}
		}); 
		
	}
	function pushStockpiling(row){
		var stockQty = $('#'+row+'stockQty').val();
		 var reg = /^[0-9]+$/;
		if(!reg.test(stockQty)){
			setMessage("alert-warning","请先正确填写囤货数量");
			$('#'+row+'stockQty').attr("border-color","red");
			return;jstl;
		}else{
			$('#'+row+'stockQty').attr("border-color","");
		}
		setMessage("alert-success","提交请求中......");
		
		var url = serverpath+"/quotationmerchandise/upMerchandise/"+quotationId+"/"+row;
		$.ajax({
			type: "POST",
			url:url,
	        dataType:'text',
	        data: {'status':'4','stockQty':stockQty},
			success: function(data){
				if("success"==data){
					setMessage("alert-success","提交成功，等待审核！2秒后将刷新页面");
					window.setTimeout(function (){
		       			window.location.href=serverpath+"/quotationmerchandise/list/"+quotationId+"/"+$('#skuStatu').val();
		       		},2000);
				}else{
					setMessage("alert-danger","操作失败！");
				}
	       		
	      	}
		}); 
		
	}
	
	function savemerchandise(obj){
		if(!saveFlag){
			setMessage("alert-warning","请检查输入是否正确");
			return;
		}
		setMessage("alert-success","正在保存......");
		var row = obj.closest('.merch');
		var rowdata='';
		for (j = 5; j < row.cells.length; j++) {
			var input = row.cells.item(j).childNodes[0];
			if(typeof(input)!= "undefined"){
				if(""==input.value)rowdata+='undefined::';
				else rowdata+=input.value+'::';
			}else{
				rowdata+='undefined::';
			}
	    }
		var url = serverpath+"/quotationmerchandise/addMerchandise/"+quotationId;
		$.ajax({
			type: "POST",
			url:url,
			data:{"data":rowdata},
	        dataType:'text',
			success: function(data){
				if("success"==data){
					setMessage("alert-success","保存成功！2秒后将刷新页面");
		       		window.setTimeout(function (){
		       			window.location.href=serverpath+"/quotationmerchandise/list/"+quotationId+"/"+$('#skuStatu').val();
		       		},2000);
				}else{
					setMessage("alert-danger","操作失败！");
				}
	       		
	      	}
		}); 
	}
	
	function quotationTrate(){
		window.location.href = serverpath+"/quotationmerchandise/quotationTrate/"+quotationId;
	}
	function skuStatuChange(index){
		var value = $('#'+index+'statuSelect').find("option:selected").val();//选中的值
		$('#'+index+'skuStatus').val(value);
	}
	function validate(obj){
	    var reg = /^[0-9]+\.?[0-9]*$/;
	    if(!reg.test(obj.value)){
	    	saveFlag = false;
	    	obj.style.borderColor="red";
	    	 jfslk;
	    } else{
	    	saveFlag = true;
	    	obj.style.borderColor="";
	    }
	}  
	
	function europePrice(row){
		 getEuropeRMB(row);
		 getRatio(row);
	}
	function officePriceChange(row){
		 getDiscount(row);
	}
	function skuPriceChange(row){
		getDiscountPrice(row);
		getDiscount(row);
		getRatio(row);
	}
	
	function getDiscountPrice(row){
		var price = $('#'+row+'price').val();
		if(price!=""){
			$('#'+row+'discountPrice').val(Math.round(Number(price)*0.95 * 100) / 100);
		}
	}
	function getDiscount(row){
		var officePrice = $('#'+row+'officePrice').val();
		var price = $('#'+row+'price').val();
		if(price!=""&&officePrice!=""&&Number(officePrice)!=0){
			var discount = Number(price)/Number(officePrice);
			$('#'+row+'discount').val(Math.round(discount * 100) / 100);
		}
	}
	function getEuropeRMB(row){
		var retail = $('#'+row+'retail').val();
		var rate = $('#'+row+'rate').val();
		if(retail!=""&&rate!=""){
			$('#'+row+'europePriceRmb').val(Math.round(Number(retail)*Number(rate)* 100)/ 100);
		}
	}
	function getRatio(row){
		var europePriceRmb = $('#'+row+'europePriceRmb').val();
		var price = $('#'+row+'price').val();
		var discountPrice = $('#'+row+'discountPrice').val();
		if(price!=""&&europePriceRmb!=""&&Number(europePriceRmb)!=0){
			var ratio = Number(price)/Number(europePriceRmb);
			$('#'+row+'ratio1').val(Math.round(ratio * 100) / 100);
		}
		if(discountPrice!=""&&europePriceRmb!=""&&Number(europePriceRmb)!=0){
			var ratio = Number(discountPrice)/Number(europePriceRmb);
			$('#'+row+'ratio2').val(Math.round(ratio * 100) / 100);
		}
	}
	
	function setMessage(classstr,message){
		$('#message').html(message);
		$('#messageDiv').attr("class","alert alert-dismissable").addClass(classstr);
		$('#messageDiv').show();
	}
	
	function checkAll(obj){
		if($(obj).is(':checked')){
			$("input[name='ids']").attr("checked",true);
		}else{
			$("input[name='ids']").attr("checked",false);
		}
		
	}
	function checkOne(obj){
		$(obj).attr("checked",true);
	}
	function getReviewList(status){
		 window.location.href=serverpath+"/merchandise/list/"+quotationId+"/"+status;
	}
	function backorder(skuSatu){
		 var idsStr = '';
		 $("[name='ids'][checked]").each(function(){
			 idsStr +=  $(this).val()+"_";
		 });
		 if(idsStr==''){
			 setMessage("alert-warning","请先选择数据");
			 jfslk;
		 }
		 setMessage("alert-success","处理中......");
		 var url = serverpath+"/merchandise/back/"+quotationId+"/"+skuSatu;
			$.ajax({
					type: "POST",
					url:url,
					data:{"ids":idsStr},
			        type:'post',
			        dataType:'text',
					success: function(data){
						if("success"==data){
							setMessage("alert-success","成功！2秒后更新页面");
				       		window.setTimeout(function (){
				       		 	window.location.href=serverpath+"/merchandise/list/"+quotationId+"/"+skuSatu;
				       		} ,2000);
						}else{
							setMessage("alert-danger","操作失败！");
						}
						
			      	}
			});  
		
		
  }
	function pushOnsale(){
		   var idsStr = '';
			 $("[name='ids'][checked]").each(function(){
				 idsStr +=  $(this).val()+"_";
			 });
			 if(idsStr==''){
				 setMessage("alert-warning","请先选择数据");
				 jfslk;
			 }
			 setMessage("alert-success","发布中......");

			var url = serverpath+"/merchandise/push/"+quotationId+"/4";
			$.ajax({
					type: "POST",
					url:url,
					data:{"ids":idsStr},
			        type:'post',
			        dataType:'text',
					success: function(data){
						if("success"==data){
							setMessage("alert-success","发布成功！2秒后更新页面");
				       		window.setTimeout(function (){
				       		 	window.location.href=serverpath+"/merchandise/list/"+quotationId+"/3";
				       		} ,2000);
						}else{
							setMessage("alert-danger","操作失败！");
						}
						
			      	}
			});  
	   }
	function pushStocking(){
		var stockQtys ="";
		var valiFlag = true;
		 var idsStr = '';
		$("input[name=ids]").each(function(){
			var row = $(this).val();
			var qty = $('#'+row+'summarySkuQty').val();
			var reg=new RegExp("^\\d*$");
			if(!reg.test(qty)){
				valiFlag = false;
			}
			idsStr +=  row + "_";
			if(qty!="")stockQtys += row+":"+qty+"_";
		});
		if(!valiFlag||stockQtys==""){
			 setMessage("alert-warning",'异常，囤货数量格式不正确。');
			 return;
		}
		setMessage("alert-warning",'囤货处理中......');
		var url = serverpath+"/merchandise/push/"+quotationId+"/2?stockQtys="+stockQtys;
		$.ajax({
			type: "POST",
			url:url,
			data:{"ids":idsStr},
	        dataType:'text',
			success: function(data){
				if("success"==data){
					setMessage("alert-success","囤货成功！2秒后刷新界面。");
					window.setTimeout(function (){
		       		 	window.location.href=serverpath+"/merchandise/list/"+quotationId+"/1";
		       		} ,2000);
				}else{
					setMessage("alert-danger","操作失败!");
				}
	       		
	      	}
		}); 
	}
	